<?php
/**
 * Created by PhpStorm.
 * User: bzing
 * Date: 2017-08-09
 * Time: 09:54
 */
namespace app\api\controller;
use app\models\Models;

class Withdraw extends Base{
    /**
     * 提现
     * @return \think\response\Json
     */
    public function getCash(){
        if(request()->isPost()){
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt']);
                $account = new Models('account');
                $acc_info = $account->_find(['uid'=>$params['uid']]);
                if($acc_info['balance'] < $params['money']){
                    throw new \LogicException('账户余额不足',1039);
                }
                $withdraw = new Models('withdraw');
                $add['uid'] = $params['uid'];
                $add['order_no'] = date('YmdHis').rand(0001,9999);
                $add['money'] = $params['money'];
                $add['create_time'] = time();
                // 开启事物
                $account->startTrans();
                // 提现表添加记录
                $bool = $withdraw->_add($add);
                // 操作相关金额
                $bool1 = $account->_save(['balance'=>['exp','balance-'.$params['money']],'frozen'=>['exp','frozen+'.$params['money']]],['uid'=>$params['uid']]);
                if(!$bool || !$bool1){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                $account->rollback();
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            $account->commit();
            return json(self::formatSuccessResult());
        }
    }
}